package com.aplink.generic.widgets;

import java.io.Serializable;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.DatabaseGet;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ValueList;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.util.objectrule.EnableCondition;

public abstract class ApLinkBaseView implements Serializable {
	private static final long serialVersionUID = 2494167961415162834L;
	private boolean hasValueList = false;
	private final ControlSpec mControlSpec;
	private final GenericFragment mActivity;
	private String mRefreshValue = null;
	private View mSubmitControl;
	private View mValueControl;
	protected ValueList mValueList;
	
	public ApLinkBaseView(final GenericFragment genericFragment,
			final ControlSpec controlSpec, final View view) {
		this.mActivity = genericFragment;
		this.mControlSpec = controlSpec;
		initValueList();
	}
	
	public abstract void clear();
	
	protected View findViewById(final int id) {
		return getActivity().findViewById(id);
	}
	
	protected Activity getActivity() {
		return this.mActivity.getActivity();
	}
	
	protected Context getApplicationContext() {
		return this.mActivity.getApplicationContext();
	}
	
	public abstract GenericAttribute getAttribute();
	
	protected GenericObject getBindingObject() {
		return this.mActivity.getBindingObject();
	}
	
	public ControlSpec getControlSpec() {
		return this.mControlSpec;
	}
	
	protected DatabaseGet getDatabaseGet() {
		return this.mActivity.getDatabaseGet();
	}
	
	protected EnableCondition getEnableCondition() {
		return this.mControlSpec.getEnableCondition();
	}
	
	protected FragmentManager getChildFragmentManager() {
		return this.mActivity.getChildFragmentManager();
	}
	
	public GenericFragment getGenericFragment() {
		return this.mActivity;
	}
	
	protected GenericDictionary<String, String> getHashPassedValues() {
		return this.mActivity.getHashPassedValues();
	}
	
	public abstract GenericDictionary<String, GenericAttribute> getListAttributes();
	
	protected String getPassedContent() {
		final String columnName = this.mControlSpec.getDataObject() + "."
				+ this.mControlSpec.getMappingName();
		if (getPassedGenericObject().containsKey(columnName)) {
			final String value = getPassedGenericObject().getValue(columnName);
			if (!TextUtils.isEmpty(value)) {
				return value;
			}
		}
		return null;
	}
	
	public GenericObject getPassedGenericObject() {
		return this.mActivity.getPassedGenericObject();
	}
	
	public String getRefreshConditions() {
		return this.mRefreshValue;
	}
	
	protected Resources getResources() {
		return this.mActivity.getResources();
	}
	
	protected View getSubmitControl() {
		return this.mSubmitControl;
	}
	
	protected View getValueControl() {
		return this.mValueControl;
	}
	
	public abstract View getView();
	
	public boolean hasValueList() {
		return this.hasValueList;
	}
	
	private void initValueList() {
		if ((this.mControlSpec.getValueList() != null)
				&& !this.mControlSpec.getValueList().isEmpty()) {
			final ValueList valueList = this.mControlSpec.getValueList();
			final int valueControldID = ResourcesHelper
					.getViewIdByName(valueList.getValueControl());
			this.mValueControl = getActivity().findViewById(valueControldID);
			final int submitControlID = ResourcesHelper
					.getViewIdByName(valueList.getSubmitControl());
			this.mSubmitControl = getActivity().findViewById(submitControlID);
			this.hasValueList = true;
			this.mValueList = this.mControlSpec.getValueList();
		}
	}
	
	public void initView() {
		initViewProperties();
		notifyDataSetChanged();
		setEventListener();
	}
	
	protected abstract void initViewProperties();
	
	protected abstract void loadBindingObject();
	
	protected abstract void loadDefaultObject();
	
	public void notifyDataSetChanged() {
		if (getHashPassedValues().isEmpty()) {
			loadDefaultObject();
		} else {
			loadBindingObject();
		}
	}
	
	protected abstract void setEventListener();
	
	public abstract void setObjPropInfoTemp(OnGetObjProInfo getObjProInfo);
	
	public abstract void setOnClickListener(OnClickListener onClickListener);
	
	public void setRefreshConditions(final String refreshValue) {
		this.mRefreshValue = refreshValue;
	}
	
	@Override
	public String toString() {
		return this.mControlSpec.getIDName();
	}
	
}
